package com.yuxi.controller.manager;
import com.yuxi.common.SecurityUtil;
import com.yuxi.common.SkJsonResult;
import com.yuxi.common.exception.ExceptionMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.pubInter.IBaseService;
import com.yuxi.pubInter.RestfulController;
import com.yuxi.dto.AdminDTO;
import com.yuxi.dto.AdminPageDTO;
import com.yuxi.modelExt.AdminExt;
import com.yuxi.searchExt.AdminSearchExt;
import com.yuxi.service.IAdminService;
import com.yuxi.vo.AdminDetailVO;
import com.yuxi.vo.AdminListVO;
import com.yuxi.vo.AdminDomainVO;
import com.yuxi.vo.AdminDetailDomainVO;

import org.springframework.validation.annotation.Validated;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import com.yuxi.common.IdGen;
import com.yuxi.common.StringUtil;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import com.yuxi.pubInter.exception.BusinessException;
import com.yuxi.vo.RightsDetailDomainMiniVO;


/**
*    @author sunke
*    @Date 2023-10-13 10:28:17
*    @description AdminController      系统用户
*/
@Api(tags = "后台管理系统-系统用户相关", description = "系统用户相关接口")
@RestController(value ="adminRestfulController")
@RequestMapping("/manager/admin/")
@Validated
public class AdminController extends ApiManageController<AdminDTO,AdminPageDTO,AdminExt, AdminSearchExt,AdminDomainVO,AdminDetailDomainVO,AdminDetailVO,AdminListVO,String> {

	@Resource(name = "adminService")
	private IAdminService adminService;



	@Override
	public IBaseService<AdminExt, AdminSearchExt,String> getBaseService() throws SkException {
		return adminService;
	}


    @Override
    public boolean beforePostAdd(AdminDTO dto, AdminExt domain, HttpServletRequest request, HttpSession session) throws SkException {


        String username = domain.getUsername();

        domain.setId(IdGen.uuid());
        domain.setPassword(SecurityUtil.generateUserPasswordByPswAndSalt(domain.getPassword(), domain.getId()));

        if (StringUtil.isNotObjEmpty(adminService.getInfoByUsername(username))) {
            throw new BusinessException(ExceptionMessage.EXIST_CLIENT);
        }


        return super.beforePostAdd(dto, domain, request, session);
    }

    @ApiOperation(value = "我的权限", notes = "我的权限接口", httpMethod = "POST")
    @PostMapping("myRights")
    public SkJsonResult<List<RightsDetailDomainMiniVO>> myRights(HttpServletRequest request) throws SkException {
        return SkJsonResult.ok().setData(mapList(adminService.getAllRightsListByStaffId(getJwtsUtil().getAdminId(request), true), RightsDetailDomainMiniVO.class));
    }

	

	@Override
	public String getBaseRoute() {
		return "/manager/admin/";
	}

	/**
	* 当前模板名称
	* @return
	*/
	@Override
	public String getModelName(){
		return "系统用户";
	}



	@Override
	public Set<String> totalMapSet() {
		Set totalMapSet=new HashSet();
		totalMapSet.add("count(admin.a_id) as countNum");
		return totalMapSet;
	}


}